package com.shengde.jinwawaforleader.base;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;

import com.shengde.jinwawaforleader.HomeApplication;
import com.shengde.jinwawaforleader.utils.ActivityHelper;
import com.shengde.jinwawaforleader.utils.ActivityStackUtil;

/**
 * Created by Chason on 2016/9/8.
 */

public abstract class BaseFragmentActivity extends FragmentActivity implements
        View.OnClickListener{
    // 回调跳转界面
    private BaseActivity.IntentCallBack intentCallBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        onRoot(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ActivityStackUtil.add(this);
//       HomeApplication.addActivity(this);
        intentCallBack = new BaseActivity.IntentCallBack() {
            @Override
            public Intent startActivityCommon(Class cls) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), cls);
                startActivity(intent);
                return intent;
            }

            @Override
            public Intent startActivityForResultCommon(Class cls, int result) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), cls);
                startActivityForResult(intent, result);
                return intent;
            }

            @Override
            public Intent startActivityFinishCommon(Class cls) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), cls);
                startActivity(intent);
                finish();
                return intent;
            }
        };
        initView();
        initData();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityStackUtil.remove(this);
    }

    @Override
    public void onClick(View v) {
        onWidgetClick(v);
    }

    public BaseActivity.IntentCallBack getIntentCallBack() {
        return intentCallBack;
    }

    public interface IntentCallBack {
        Intent startActivityCommon(Class cls);

        Intent startActivityForResultCommon(Class cls, int result);

        Intent startActivityFinishCommon(Class cls);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        return ActivityHelper.createLoadingDialog(this);
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        return ActivityHelper.createLoadingDialog(this);
    }

    // 设置内容
    public abstract void onRoot(Bundle savedInstanceState);

    // 初始化控件
    public abstract void initView();

    // 初始化数据
    public abstract void initData();

    // 初始化控件点击事件
    public abstract void initListener();

    // 初始化控件点击事件
    public abstract void onWidgetClick(View view);
}
